package com.coded2.mobileposto.bean;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;

import javax.enterprise.context.SessionScoped;
import javax.faces.application.FacesMessage;
import javax.inject.Inject;
import javax.inject.Named;

import com.coded2.infra.comum.UtilBean;
import com.coded2.mobileposto.model.Combustivel;
import com.coded2.mobileposto.service.ManterCombustivelService;



@Named
@SessionScoped
public class ManterCombustivelBean implements Serializable{

	/**
	 * 
	 */
	private static final long serialVersionUID = -7860192088444307754L;
	
	@Inject
	private ManterCombustivelService service;
	
	private Combustivel combustivel;
	
	private List<Combustivel> combustiveis; 
	
	public String create(){
		
		setCombustiveis(null);
		setCombustivel(null);
		
		return "/view/ManterCombustivel/create.xhtml";
	}
	
	public String list() throws Exception{
		setCombustiveis(service.search(new Combustivel()));
		return "/view/ManterCombustivel/list.xhtml";
		
	}
	

	public String save(){
	
		List<String> erros = service.save(getCombustivel());
		if(erros!=null && !erros.isEmpty()){
			for (String erro : erros){
				UtilBean.addErrorMessage(erro);
			}
			return UtilBean.ACTION_CREATE;
		}
		UtilBean.addInfoMessage("Registro gravado com sucesso");
		return UtilBean.ACTION_EDIT;
		
	}
	
	public String edit() throws Exception{
		
		setCombustivel(service.searchByID(combustivel));
		
		return UtilBean.ACTION_EDIT;
	}
	
	
	
	public String update(){
		
		List<String> erros = service.update(getCombustivel());
		if(erros!=null && !erros.isEmpty()){
			for (String erro : erros){
				UtilBean.addErrorMessage(erro);
			}
					}
		UtilBean.addInfoMessage("Registro alterado com sucesso");

		
		return UtilBean.ACTION_EDIT;
		
	}
	
	public Combustivel getCombustivel() {
	
		if(this.combustivel==null){
			combustivel = new Combustivel();
		}
		
		return combustivel;
	}

	public void setCombustivel(Combustivel combustivel) {
		this.combustivel = combustivel;
	}

	public List<Combustivel> getCombustiveis() {
		if(this.combustiveis==null){
			combustiveis = new ArrayList<Combustivel>();			
		}
		return combustiveis;
	}

	public void setCombustiveis(List<Combustivel> combustiveis) {
		this.combustiveis = combustiveis;
	}
	
	public String remove() throws Exception{
		List<String> erros = service.remove(getCombustivel());
		setCombustivel(null);
		return list(); 
	}
	
	
	
}
